package com.wy2.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.wy2.entity.Clock;
import com.wy2.mapper.ClockMapper;
import com.wy2.service.ClockService;
import com.wy2.util.PoiUtils;

@RestController
@RequestMapping("/excel")
public class ExcelController {
	
	@Autowired
	private ClockMapper cm;
	@Autowired   
	private ClockService cs;
	@GetMapping
	public void testExcel(HttpServletResponse response, String ids) {
		System.out.println("====="+ids);
		String downloadName = "clock.xls";
		response.setCharacterEncoding("UTF-8");// 设置响应的字符编码格式
		response.setContentType("application/vnd.ms-excel");// 指明响应文件为excel类型
		ServletOutputStream out = null;
		try {
			// 文件名编码处理，防止浏览器下载文件名乱码
			response.setHeader("Content-disposition", "attachment;filename="
					+ new String(downloadName.getBytes("GB2312"), "ISO8859-1"));
			out = response.getOutputStream();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		//1、通过前端传来的数据生成Excel
		//2、通过前端传了来的id
		
		List<Clock> sList = new ArrayList<Clock>();
		String[] mid=ids.split(","); 
		System.out.println("mid"+mid.length);
		if(ids !=null && ids != "") {
			for(int i =0;i<mid.length;i++) {
				//同过遍历取出字符串里的每个id
				String id=mid[i];
				List<Clock> sin = cm.findById(id);
				
			// stu.get().setMemberships(stu.get().getMemberships().getDepartment());  	;
				sList.add(sin.get(0));
			}
			
		}else {
			sList = cm.findAll();
			System.out.println("slist====="+sList);
		}
		
		try {
			PoiUtils p = new PoiUtils();
			System.out.println("pppppppppppppp"+p);
			p.createExcel(sList, Clock.class, out);
			System.out.println(1);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 导入时名称依旧为唯一标识，因此不能重复
	 * @param file
	 * @return
	 */
	@RequestMapping("/importExcel")
	public Map<String, Object> importExcel(@RequestParam(value = "file") MultipartFile file) {
		System.out.println("123");
		Map<String, Object> map=new HashMap<String, Object>();
		String originalFilename = file.getOriginalFilename();// 得到上传文件的名称
		//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//把上传的Excel文件里的数据解析出来，调用方法，存入数据库
		PoiUtils poiUtils = new PoiUtils();// Excel工具类
		List<Clock> list = null;
		Integer singin = null;
		try {
			list = (List<Clock>) poiUtils.parseExcel(Clock.class, file.getInputStream(), originalFilename);
			for (Clock s : list) {
				singin = cs.importSingin(s);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		if(singin!=null) {
			map.put("code", 0);
			return map;
		}
		map.put("code", -1);
		return   map;
	}
	
	
	
	
	
	
	
	
	
	
}
